/**
 * Created by WT-SUN on 2017/4/20.
 */
$(function () {
    $('#phyDataTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/phyData/phyTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc',
            isAbnormal:'1'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'userPhy', title: '姓名', width: 100, formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'   onclick='infoAb(\""+row.id+"\");'>"+row.userPhy+"</a>";//'<a href="javascript:void(0)" class="easyui-linkbutton" onclick="infoPhyData('+row.id+')">'+row.userPhy+'</a>'
            }},
            {
                field: 'CompanyName',
                title: '所属部门',
                width: 100,
                formatter: function (value, row, index) {
                    var name = '';
                    if (row.userDepartmentList && row.userDepartmentList.length > 0) {
                        for (var index in row.userDepartmentList) {
                            if (row.userDepartmentList[index].deptName != null) {
                                name += ',' + row.userDepartmentList[index].deptName;
                            }
                        }
                        return name.substring(1);
                    } else {
                        return '';
                    }
                }
            },
            {field: 'batch', title: '体检批次', width: 100},
            {field: 'yearAnnual', title: '年度', width: 100},
            {field: 'planType', title: '体检类别', width: 100},
            {field: 'physicalDate', title: '体检日期', width: 100},
            {field: 'conclusion', title: '体检结论', width: 100,formatter: function (value, row, index) {
                var name = '';
                if (row.phyConclusions && row.phyConclusions.length > 0) {
                    for (var index in row.phyConclusions) {
                        if (row.phyConclusions[index].conName != null) {
                            name += ',' + row.phyConclusions[index].conName;
                        }
                    }
                    return name.substring(1);
                } else {
                    return '';
                }
            }},
            {field: 'reConclusion', title: '复查结论', width: 100,formatter: function (value, row, index) {
                var name = '';
                if (row.phyReview && row.phyReview.length > 0) {
                    for (var index in row.phyReview) {
                        if (row.phyReview[index].reConclusionName != null) {
                            name += ',' + row.phyReview[index].reConclusionName;
                        }
                    }
                    return name.substring(1);
                } else {
                    return '';
                }
            }},
            {field: 'reState', title: '处理状态', width: 100,formatter: function (value, row, index) {
                var name = '';
                if (row.phyConclusionsState && row.phyConclusionsState.length > 0) {
                    for (var index in row.phyConclusionsState) {
                        if (row.phyConclusionsState[index].conName != null) {
                            name += ',' + row.phyConclusionsState[index].conName;
                        }
                    }
                    return name.substring(1);
                } else {
                    return '';
                }
            }},
            {field: 'rezd', title: '职业病诊断结论', width: 100,formatter: function (value, row, index) {
                var name = '';
                if (row.phyDiagnosis && row.phyDiagnosis.length > 0) {
                    for (var index in row.phyDiagnosis) {
                        if (row.phyDiagnosis[index].conclusionName != null) {
                            name += ',' + row.phyDiagnosis[index].conclusionName;
                        }
                    }
                    return name.substring(1);
                } else {
                    return '';
                }
            }},
            {field: 'postName', title: '岗位', width: 100},
            {field: 'jhys', title: '接害因素', width: 100,formatter: function (value, row, index) {
                var name = '';
                if (row.phyHarmIds && row.phyHarmIds.length > 0) {
                    for (var index in row.phyHarmIds) {
                        if (row.phyHarmIds[index].name != null) {
                            name += ',' + row.phyHarmIds[index].name;
                        }
                    }
                    return name.substring(1);
                } else {
                    return '';
                }
            }},
            {field: 'userName', title: '登记人', width: 100},
            {field: 'deptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100}
        ]]
    });
    /*$('#btn_phyData_add').on('click',function () {
        addPhyData();
    });*/
    /*$('#btn_phyData_delete').on('click',function () {
        deletePhyData();
    });*/
    $('#btn_phyDataYc_edit').on('click',function () {
       editPhyData();
     });
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#phyDataTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
        $('#nowPosition').combobox({
            data:[],
            require:true
        });
    });
    queryYearthList("yearAnnual");
    var code=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_CONCLUSION;
    queryComboData(code,'conclusion');
    var code2=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_TYPE;
    queryComboData(code2,'medicalCategory');
    var code3=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_REVIEWINFO;
    queryComboData(code3,'reconclusion');
    var code4=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_OPTIONS;
    queryComboData(code4,'conclusionIdState');
    var isZyb=[{id:'',text:'-全部-'},{id:'0',text:'是'},{id:'1',text:'否'}];
    $("#isZyb").combobox({
        data: isZyb,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '150',
        selectOnNavigation:true
    });
    queryDepartmentTree();
    $("#searchAddUser").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }]
    });
    /*$('#nowPosition').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.USER_POST,
        valueField: 'id',
        textField: 'text',
        loadFilter: function (data) {
            data.unshift({
                id: '',
                text: '请选择'
            });
            return data;
        }
    });*/
});
var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#searchAddUser").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
};
/**
 * 年度
 * @param id
 */
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            editable:false,
            valueField:'id',
            textField:'text'
        }
    );
}
function queryComboData(code,id) {
    var comId='#'+id;
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(comId).combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
function queryDepartmentTree(){
    $('#nowPosition').combobox({
        valueField: 'id',
        textField: 'text',
        prompt:'请选择'
    });
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){

            deptPost('searchDept',result);
            deptCom('searchAddDept',result);
        }
    });
}
function deptPost(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            require:true,
            prompt:'请选择',
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            },
            onSelect:function (record){
                $.ajax({
                    url:ROOT_PATH + '/permission/post/findByDeptId.shtml?deptId='+record.id,//提交地址
                    type:"POST",
                    dataType:"json",
                    success:function(result){
                        $('#nowPosition').combobox({
                            data:result,
                            require:true
                        });
                    }
                });
            },
            onChange:function (newValue,oldValue) {
            }
        }
    );
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            require:true,
            prompt:'请选择',
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#phyDataTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    queryParams["isAbnormal"] = '1';
    $('#phyDataTb').datagrid('options').queryParams = queryParams;
    $("#phyDataTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/phyData/phyTable.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    parent.layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['1190px', '80%'],
        content: content
    });
}

/**
 * 编辑
 */
function editPhyData() {
    var rows = $('#phyDataTb').datagrid('getSelections');
     if (rows.length == 0) {
     layer.alert("选择需要编辑的行！");
     return
     }
    var id = rows[0].id;
    var title = '体检异常处置';
    var content = ROOT_PATH + '/occupationalHealth/phyData/abnormal.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=edit';
    openLayers(title, content);
}
/**
 * 体检异常详情
 */
function infoAb(id) {
    var title = '体检异常处置详情';
    var content = ROOT_PATH + '/occupationalHealth/phyData/abnormal.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=info';
    openLayers(title, content);
}

